﻿myApp.controller('JoinTeamController', function ($http, $scope, $location, authService, $timeout) {
    $scope.$emit('newPageLoaded', {
        'title': 'Join Us | ShiJie Property',
        'description': "Join Us. We Work Very Closely With Developers. Over 40 Years Experience. Join One Of The Most Established Real Estate Company In Penang!",
        'keywords': 'join us, join shijie property, join our team'
    });


    //$scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 CAREER', 'description': 'Earn more than 10K per month, Join us!' });


    $scope.close = function () {
        $scope.warning = null;
        $scope.error = null;
        $scope.info = null;
    }

    function pageInit() {
        // get description
        $http.get('api/Career/GetJobPositions')
             .success(function (data) {

                 $scope.job = data;
             }
        )

        $http.get('api/Career/GetJobDescription')
            .success(function (data) {

                $scope.description = data;
            }
        )
        // get job vacancy

        $scope.loader = { loading: false };

    }
    pageInit();
    $scope.person = {
        "gender": ["Male", "Female"]
    };

    $scope.property = {};
    $scope.person.gender = "M";
    $scope.genderName = "Male";
    $scope.person.position = "Admin Staff";
    $scope.position = "Admin Staff";


    $scope.updatePosition = function (positionType, positionChanged) {
        $scope.positionChanged = positionChanged;
        $scope.property.position = positionType;
        $scope.position = positionType;
    }

    $scope.updateGender = function (gender, genderChanged) {
        $scope.genderChanged = genderChanged;
        $scope.property.gender = gender;
        switch (gender) {
            case 'M':
                $scope.genderName = "Male";
                break;
            case 'F':
                $scope.genderName = "Female";
                break;
        }
    }

   


    $scope.save = function () {
        $scope.loader.loading = true;


        window.scrollBy(0, -10000);
        if (!validation())
            return;

        url = 'api/Career/save';

        console.log("CAREER SAVE: " + JSON.stringify($scope.person));
        $http.post(url, $scope.person)
             .success(function (result) {
                 $scope.loader.loading = false;
                 $scope.data.searchData = result;
                 $scope.info = "Your request has been submitted successfully. We thank you for your application.";

             })
             .error(function (result) {
                 $scope.loader.loading = false;
                 $scope.error = "Your request cannot be submitted now. Please contact our office directly for us to liase with you immediately.";
             }
         )
    }


    $scope.clearEntry = function () {
        $scope.person.name = "";
        $scope.person.phone = "";
        $scope.person.gender = "";
        $scope.person.ic = "";
        $scope.person.address = "";
        $scope.person.position = "";
        $scope.close();
    }

    $scope.close = function () {
        $scope.warning = null;
        $scope.info = null;
        $scope.error = null;
    }

    function validation() {
        $scope.warning = [];

        if (!$scope.person.name)
            $scope.warning.push({ msg: "Name must not be empty!" });
        if (!$scope.person.phone)
            $scope.warning.push({ msg: "Phone must not be empty!" });
        if (!$scope.person.ic)
            $scope.warning.push({ msg: "IC must not be empty!" });

        if ($scope.warning.length > 0)
            return false;
        else {
            $scope.warning = null;
            return true;
        }
    }

});